import Server from './server'
import { getUrlConcat, getStore } from '../utils/commons'
import { message } from 'antd';


class API extends Server {
  /**
   *  错误码处理
   *  @param {string} code
   *  @return {function}
   */
  dealEorr = (code) => {
    if (getStore('errorCode') && code) {
      if (code === 1) {
        window.localStorage.removeItem('userInfo')
        window.localStorage.removeItem('token')
        if (window.location.hash.search('#/login') === -1 && window.location.hash.search('#/register') === -1) {
          window.location.reload();
        }
      }
      let errorCode = JSON.parse(getStore('errorCode'))
      errorCode['9999'] = '接口返回错误'
      message.warning(errorCode[code])
      return code
    } else {
      message.warning('网络错误,错误码' + code)
      return code
    }
  }

  /**
   *  获取错误码
   *  @url {{url}}/api/app/codes
   *  @method get
   *  @return {promise}
   */
  async getErrorCode() {
    try {
      let result = await this.axios('get', '/app/code', {})
      console.log(result)

      if (result.code === 0 && (result instanceof Object)) {
        return result.data || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  获取区号
   *  @url {{url}}/api/auth/country
   *  @method get
   *  @return {promise}
   */
  async getRegister() {
    try {
      let result = await this.axios('get', '/auth/country', {})
      if (result.code === 0 && (result instanceof Object)) {
        return result.data || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  获取行为验证码
   *  @url {{url}}/api/app/verify
   *  @method get
   *  @return {promise}
   */
  async getVerify() {
    try {
      let result = await this.axios('get', '/app/verify', {})
      if (result.code === 0 && (result instanceof Object)) {
        return result.data || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  注册
   *  @url {{url}}/api/auth/register
   *  @method post
   *  @return {promise}
   */
  async register(data) {
    try {
      let result = await this.axios('post', '/auth/register', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  发送验证码
   *  @url {{url}}/api/app/verify
   *  @method post
   *  @return {promise}
   */
  async sendVerify(data) {
    try {
      let result = await this.axios('post', '/app/verify', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  登录
   *  @url {{url}}/api/auth/login
   *  @method post
   *  @return {promise}
   */
  async login(data) {
    try {
      let result = await this.axios('post', '/auth/login', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  重置密码
   *  @url {{url}}/api/auth/resetPassword
   *  @method post
   *  @return {promise}
   */
  async resetPassword(data) {
    try {
      let result = await this.axios('post', '/auth/resetPassword', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-各绑定情况
   *  @url {{url}}/api/user/verify
   *  @method get
   *  @return {promise}
   */
  async userVerify(data) {
    try {
      let result = await this.axios('get', '/user/verify' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-邮箱绑定
   *  @url {{url}}/api/user/verify/bind/email
   *  @method post
   *  @return {promise}
   */
  async bindEmail(data) {
    try {
      let result = await this.axios('post', '/user/verify/bind/email', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-邮箱解绑
   *  @url {{url}}/api/user/verify/unbind/email
   *  @method post
   *  @return {promise}
   */
  async unBindEmail(data) {
    try {
      let result = await this.axios('post', '/user/verify/unbind/email', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-手机绑定
   *  @url {{url}}/api/user/verify/bind/email
   *  @method post
   *  @return {promise}
   */
  async bindPhone(data) {
    try {
      let result = await this.axios('post', '/user/verify/bind/phone', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-手机解绑
   *  @url {{url}}/api/user/verify/unbind/phone
   *  @method post
   *  @return {promise}
   */
  async unBindPhone(data) {
    try {
      let result = await this.axios('post', '/user/verify/unbind/phone', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-googleAuth绑定
   *  @url {{url}}/api/user/verify/bind/googleAuth
   *  @method post
   *  @return {promise}
   */
  async bindGoogle(data) {
    try {
      let result = await this.axios('post', '/user/verify/bind/googleAuth', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-googleAuth解绑
   *  @url {{url}}/api/user/verify/unbind/googleAuth
   *  @method post
   *  @return {promise}
   */
  async unBindGoogle(data) {
    try {
      let result = await this.axios('post', '/user/verify/unbind/googleAuth', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-获取googleAuth
   *  @url {{url}}/api/user/verify/bind/googleAuth
   *  @method get
   *  @return {promise}
   */
  async getGoogle(data) {
    try {
      let result = await this.axios('get', '/user/verify/bind/googleAuth' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-登录密码修改
   *  @url {{url}}/api/user/password
   *  @method post
   *  @return {promise}
   */
  async changePassword(data) {
    try {
      let result = await this.axios('post', '/user/password', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-支付密码修改
   *  @url {{url}}/api/user/password/payment
   *  @method post
   *  @return {promise}
   */
  async changePayPassword(data) {
    try {
      let result = await this.axios('post', '/user/password/payment', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-邀请好友
   *  @url {{url}}/api/user/invite
   *  @method get
   *  @return {promise}
   */
  async inviteInfo(data) {
    try {
      let result = await this.axios('get', '/user/invite' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-邀请列表
   *  @url {{url}}/api/user/invite/inviter
   *  @method get
   *  @return {promise}
   */
  async inviteList(data) {
    try {
      let result = await this.axios('get', '/user/invite/inviter' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  用户-邀请返佣列表
   *  @url {{url}}/api/user/invite/income
   *  @method get
   *  @return {promise}
   */
  async inviteIncome(data) {
    try {
      let result = await this.axios('get', '/user/invite/income' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }


  /**
   *  钱包-概况
   *  @url {{url}}/api/wallet
   *  @method get
   *  @return {promise}
   */
  async wallet(data) {
    try {
      let result = await this.axios('get', '/wallet' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  钱包-流水记录
   *  @url {{url}}/api/wallet/bills
   *  @method get
   *  @return {promise}
   */
   async walletBills(data) {
    try {
      let result = await this.axios('get', '/wallet/bills' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }  

  /**
   *  钱包-充值
   *  @url {{url}}/api/wallet/recharge
   *  @method get
   *  @return {promise}
   */
  async rechargeInfo(data) {
    try {
      let result = await this.axios('get', '/wallet/recharge' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  钱包-概况
   *  @url {{url}}/api/user
   *  @method get
   *  @return {promise}
   */
  async getUser(data) {
    try {
      let result = await this.axios('get', '/user' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  钱包-提现信息
   *  @url {{url}}/api/wallet/withdraw
   *  @method get
   *  @return {promise}
   */
  async withdrawInfo(data) {
    try {
      let result = await this.axios('get', '/wallet/withdraw' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  提现
   *  @url {{url}}/api/wallet/withdraw
   *  @method post
   *  @return {promise}
   */
  async withdraw(data) {
    try {
      let result = await this.axios('post', '/wallet/withdraw', data)
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code, true)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  商品-分类
   *  @url {{url}}/api/goods/category
   *  @method get
   *  @return {promise}
   */
  async goodsCategory(data) {
    try {
      let result = await this.axios('get', '/goods/category' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  商品-列表
   *  @url {{url}}/api/goods
   *  @method get
   *  @return {promise}
   */
  async goodsList(data) {
    try {
      let result = await this.axios('get', '/goods' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  商品-详情
   *  @url {{url}}/api/goods/detail
   *  @method get
   *  @return {promise}
   */
   async goodsDetail(data) {
    try {
      let result = await this.axios('get', '/goods/detail' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  订单-详情
   *  @url {{url}}/api/order/detail
   *  @method get
   *  @return {promise}
   */
   async orderDetail(data) {
    try {
      let result = await this.axios('get', '/order/detail' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }   

  /**
   *  订单-支付方式
   *  @url {{url}}/api/order/pay
   *  @method get
   *  @return {promise}
   */
  async payList(data) {
    try {
      let result = await this.axios('get', '/order/pay' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  订单-列表
   *  @url {{url}}/api/order
   *  @method get
   *  @return {promise}
   */
  async orderList(data) {
    try {
      let result = await this.axios('get', '/order' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  公告-详情
   *  @url {{url}}/api/app/announcement/detail
   *  @method get
   *  @return {promise}
   */
  async annoDetail(data) {
    try {
      let result = await this.axios('get', '/app/announcement/detail' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

  /**
   *  公告-列表
   *  @url {{url}}/api/app/announcement
   *  @method get
   *  @return {promise}
   */
  async annoList(data) {
    try {
      let result = await this.axios('get', '/app/announcement' + getUrlConcat(data))
      if (result.code === 0 && (result instanceof Object)) {
        return result || []
      } else {
        this.dealEorr(result.code)
      }
    } catch (err) {
      throw err
    }
  }

}

export default new API()